<?php
namespace Lib\TagLib;

use Think\Template\TagLib;

defined('THINK_PATH') or exit();

class Ty extends TagLib {

    private static $cachetime;

    public function __construct() {
        //生成js缓存编号
        if (APP_DEBUG || !($cachetime = S('jsCacheNum'))) {
            $cachetime = $_SERVER['REQUEST_TIME'];
            S('jsCacheNum', $cachetime);
        }

        if (empty(self::$cachetime)) {
            self::$cachetime = $cachetime;
        }
    }

    protected $tags = array(
        //样式短标签
        'TYcss' => array('attr' => 'file,public', 'close' => 0),
        //js短标签
        'TYjs' => array('attr' => 'file,public', 'close' => 0),
        //插件短标签
        'TYimport' => array('attr' => 'plugin,public', 'close' => 0)
    );

    /**
     * css简易标签
     * @param type $attr
     * @param type $content
     * @return string
     * @用法 <TYcss file='index,login' public='true'/>
     * file可以多个，用道号隔开，public默认为false，私有的不需要public属性
     */
    public function _TYcss($tag, $content) {
        //文件名
        $fileNameString = $tag['file'];
        //文件位置
        $public = isset($tag['public']) && $tag['public'] == 'true' ? '__PUBLIC__' : '__PRIVATE__';
        //分割字符串得到文件数组
        $fileNameArray = explode(',', $fileNameString);
        $str = '';
        foreach ($fileNameArray as $fileName) {
            $str.="<link rel=\"stylesheet\" type=\"text/css\" href=\"$public/Css/$fileName.css?v=" . self::$cachetime . "\" />\r\n\t";
        }
        return $str;
    }

    /**
     * js简易标签
     * @param type $attr
     * @param type $content
     * @return string
     * @用法 <TYjs file='index,login' public='true'/>
     * file可以多个，用道号隔开，public默认为false，私有的不需要public属性
     */
    public function _TYjs($tag, $content) {
        //文件名
        $fileNameString = $tag['file'];
        //文件位置
        $public = isset($tag['public']) && $tag['public'] == 'true' ? '__PUBLIC__' : '__PRIVATE__';
        //分割字符串得到文件数组
        $fileNameArray = explode(',', $fileNameString);
        $str = '';
        foreach ($fileNameArray as $fileName) {
            if (empty($fileName))
                continue;
            $str.="<script type=\"text/javascript\" charset=\"utf-8\" src=\"$public/Js/$fileName.js?v=" . self::$cachetime . "\"></script>\r\n\t";
        }
        return $str;
    }

    /**
     * 导入插件，如导入ueditor插件 <TYimport plugin="ueditor" public="true"/> ，可一次导入多个插件，使用逗号隔开
     * @param unknown $attr
     * @param unknown $content
     * @return string
     */
    public function _TYimport($tag, $content) {
        //获取插件名字
        $pluginNames = $tag['plugin'];
        
        //分割字符串得到插件数组
        $plugins = explode(',', $pluginNames);
        
        $returnStr = '';

        foreach ($plugins AS $plugin) {
            $pluginMethod = '_import' . ucfirst($plugin) . 'Plugin';
            
            if (!method_exists($this, $pluginMethod)) {
                
                throw_exception($plugin . '插件不存在！', 'InfoException');
            }
            
            $returnStr .= $this->$pluginMethod();
        }
        
        return $returnStr;
    }

    /**
     * 返回ueditor插件需要的文件
     */
    private function _importUeditorPlugin() {
        //插件位置
        $str = "<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/ueditor/ueditor.config.js?v=" . self::$cachetime . "\"></script>\r\n\t";
        $str.="<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/ueditor/ueditor.all.min.js?v=" . self::$cachetime . "\"></script>\r\n\t";
        return $str;
    }

    /**
     * 返回artDialog插件需要的文件
     */
    private function _importArtDialogPlugin() {
        //插件位置
        $str = "<link rel=\"stylesheet\" type=\"text/css\" href=\"__PUBLIC__/Js/artDialog/Css/twitter.css?v=" . self::$cachetime . "\" />";
        $str.="<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/artDialog/artDialog.js?v=" . self::$cachetime . "\"></script>";
        $str.="<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/artDialog/iframeTools.js?v=" . self::$cachetime . "\"></script>";
        $str.="<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/artDialog/highlight.pack.js?v=" . self::$cachetime . "\"></script>";
        $str.="<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/artDialog/javascript.js?v=" . self::$cachetime . "\"></script>";
        return $str;
    }

    /**
     * 返回swfupload插件需要的文件
     */
    public function _importSwfUploadPlugin() {
        $filePath = C('SWFUPLOAD_PATH');

        $jsFiles = traverseFolder($filePath, 'js');

        $str = '';

        foreach ($jsFiles as $value) {
            $str .= "<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/swfupload/$value?v=" . self::$cachetime . "\"></script>\r\n\t";
        }

        return $str;
    }
    
    /**
     * 返回artDialog插件需要的文件
     */
    private function _importDatepickerPlugin() {
    	//插件位置
    	$str ="<script type=\"text/javascript\" charset=\"utf-8\" src=\"__PUBLIC__/Js/datepicker/WdatePicker.js?v=" . self::$cachetime . "\"></script>";
    	return $str;
    }

    

}

?>
